Requires Scripting PRO
The BluetoothPeripheral interface represents a Bluetooth Low Energy (BLE) peripheral device. It provides properties and methods for interacting with the device, including connecting, discovering services and characteristics, reading/writing values, and subscribing to notifications.
id: stringA unique identifier (UUID string) for the peripheral. It remains constant across app launches and can be used to identify and reconnect to the device.
name: string | nullThe name of the peripheral device, or null if not available (e.g., if not broadcast).
isConnected: booleanIndicates whether the peripheral is currently connected.
true: Connected and ready for communicationfalse: Not connected or disconnectedcanSendWriteWithoutResponse: booleanIndicates whether the peripheral is ready to send write requests without waiting for a response.
true: Supports fast, unacknowledged writes (writeWithoutResponse)false: Requires response before next writeThe onReadyToSendWriteWithoutResponse callback is triggered when this changes to true.
ancsAuthorized: booleanIndicates whether the peripheral is authorized to use Apple Notification Center Service (ANCS). Relevant only for peripherals that support ANCS.
services: BluetoothService[] | nullThe list of services discovered on the peripheral. null if discoverServices() has not been called yet.
onConnected: (() => void) | nullCalled when the peripheral is successfully connected.
onDisconnected: ((error: Error | null, isReconnecting: boolean) => void) | nullCalled when the peripheral is disconnected.
error: An error object if the disconnection was unexpected, or null if intentionalisReconnecting: true if the system is attempting to reconnect automaticallyonConnectFailed: ((error: Error) => void) | nullCalled when the peripheral fails to connect.
onNameChanged: ((name: string | null) => void) | nullCalled when the peripheral's name changes.
onDiscoverServices: ((error: Error | null, services: BluetoothService[] | null) => void) | nullCalled after calling discoverServices().
onDiscoverCharacteristics: ((error: Error | null, characteristics: BluetoothCharacteristic[] | null) => void) | nullCalled after calling discoverCharacteristics().
onDiscoverIncludedServices: ((error: Error | null, services: BluetoothService[] | null) => void) | nullCalled after calling discoverIncludedServices().
onReadyToSendWriteWithoutResponse: (() => void) | nullCalled when the peripheral is ready to send more write requests without response.
readValue(characteristic: BluetoothCharacteristic): Promise<Data>Reads the value of a given characteristic.
Parameters:
characteristic: The characteristic to read fromReturns: A Promise<Data> containing the value
maxWriteValueLength(writeType: "withResponse" | "withoutResponse"): numberGets the maximum number of bytes that can be written in a single operation.
Parameters:
writeType: "withResponse" or "withoutResponse"Returns: A number representing the max write size
writeValue(characteristic, value, writeType): Promise<void>Writes data to a characteristic.
Parameters:
characteristic: Target characteristicvalue: The data to write (Data)writeType: "withResponse" or "withoutResponse"Returns: Promise<void>
subscribe(characteristic, handler): Promise<void>Subscribes to notifications or indications from a characteristic.
Requirements: The characteristic must support "notify" or "indicate"
Parameters:
characteristic: Target characteristic
handler(error, value): Callback triggered on value change
error: Error object if anyvalue: New value as DataReturns: Promise<void>
unsubscribe(characteristic): Promise<void>Unsubscribes from notifications for a characteristic.
Parameters:
characteristic: Target characteristicReturns: Promise<void>
discoverServices(serviceUUIDs?: string[]): Promise<void>Discovers the services available on the peripheral.
Parameters:
serviceUUIDs: Optional list of service UUIDs to filterReturns: Promise<void>
discoverIncludedServices(service, includedServiceUUIDs?): Promise<void>Discovers included services within a service.
Parameters:
service: The parent serviceincludedServiceUUIDs: Optional list of included service UUIDsReturns: Promise<void>
discoverCharacteristics(service, characteristicUUIDs?): Promise<void>Discovers characteristics within a service.
Parameters:
service: The target servicecharacteristicUUIDs: Optional list of characteristic UUIDsReturns: Promise<void>
readRSSI(): Promise<number>Reads the current Received Signal Strength Indicator (RSSI).
Promise<number> representing signal strength in dBmdiscoverServices() and discoverCharacteristics() before interacting with a peripheral’s data.unsubscribe() when notifications are no longer needed.canSendWriteWithoutResponse is false.